package com.jiepos.mpos.pay;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.jiepos.mpos.common.constant.SystemConstant;
import com.jiepos.mpos.core.service.BaseInterService;
import com.jiepos.mpos.inter.intopieces.dto.RequestDTO;
import com.jiepos.mpos.inter.intopieces.dto.ResponseDTO;

@Controller
@RequestMapping("httppaygate")
@SuppressWarnings("rawtypes")
public class PayGateController {
	public static final Logger _log = LoggerFactory.getLogger(PayGateController.class);

	@Autowired
	private PayService payService;
	
	@Autowired
	private BaseInterService baseInterService;
		
	/**
	 * 接口: 外接支付控制层
	 * @param request 请求对象
	 * @param requestDto  请求实体
	 * @return
	 */
	@RequestMapping(value = "/payGateRequest.do", method = RequestMethod.POST)
	public @ResponseBody ResponseDTO payGateRequest(HttpServletRequest request, RequestDTO requestDto){				
		ResponseDTO responseDto = new ResponseDTO();	
		Map<String, String> reqBizContent = null, resBizContent = null;
		try{
			_log.info("请求{}，报文：{}", "payGateRequest", requestDto.toString());
			/*提取业务请求参数*/
			reqBizContent = baseInterService.getRequestBizContent(requestDto);
			_log.info("请求{}，业务参数：{}", "payGateRequest", reqBizContent);
			resBizContent = payService.payGateService(request, requestDto, reqBizContent, responseDto);
			_log.info("{}业务处理结果：{}", "payGateRequest", resBizContent);
			/*应答参数签名加密*/
			String respData = baseInterService.getResponseBizContent(null, resBizContent);
			responseDto.setData(respData);
		}catch(Exception e){
			_log.error("快捷支付服务发生异常", e);
			responseDto.setRsp_code(SystemConstant.RSPCODE_ERROR);
			responseDto.setRsp_msg(SystemConstant.RSPCODE_ERROR_MSG);
		}finally{
			baseInterService.saveLog(requestDto, responseDto, reqBizContent, resBizContent, "payGateRequest");
		}
		_log.info("应答{}，报文：{}", "payGateRequest", responseDto.toString());
		return responseDto;
	}
	
	/**
	 * 接口: 外接支付控制层
	 * @param request 请求对象
	 * @param requestDto  请求实体
	 * @return
	 */
	@RequestMapping(value = "/sendVerifyCode.do", method = RequestMethod.POST)
	public @ResponseBody ResponseDTO sendVerifyCode(HttpServletRequest request, RequestDTO requestDto){				
		ResponseDTO responseDto = new ResponseDTO();	
		Map<String, String> reqBizContent = null, resBizContent = null;
		try{
			_log.info("请求{}，报文：{}", "sendVerifyCode", requestDto.toString());
			/*提取业务请求参数*/
			reqBizContent = baseInterService.getRequestBizContent(requestDto);
			_log.info("请求{}，业务参数：{}", "sendVerifyCode", reqBizContent);
			resBizContent = payService.sendGateVerifyCode(request, requestDto, reqBizContent, responseDto);
			_log.info("{}业务处理结果：{}", "sendVerifyCode", resBizContent);
			/*应答参数签名加密*/
			String respData = baseInterService.getResponseBizContent(null, resBizContent);
			responseDto.setData(respData);
		}catch(Exception e){
			_log.error("订单查询服务发生异常",e);
			responseDto.setRsp_code(SystemConstant.RSPCODE_ERROR);
			responseDto.setRsp_msg(SystemConstant.RSPCODE_ERROR_MSG);
		}finally{
			baseInterService.saveLog(requestDto, responseDto, reqBizContent, resBizContent, "payGateRequest");
		}
		_log.info("应答{}，报文：{}", "sendVerifyCode", responseDto.toString());
		return responseDto;
	}
	
	/**
	 * 接口: 外接支付控制层
	 * @param request 请求对象
	 * @param requestDto  请求实体
	 * @return
	 */
	@RequestMapping(value = "/confirmVerifyCode.do", method = RequestMethod.POST)
	public @ResponseBody ResponseDTO confirmVerifyCode(HttpServletRequest request, RequestDTO requestDto){				
		ResponseDTO responseDto = new ResponseDTO();	
		Map<String, String> reqBizContent = null, resBizContent = null;
		try{
			_log.info("请求{}，报文：{}", "confirmVerifyCode", requestDto.toString());
			/*提取业务请求参数*/
			reqBizContent = baseInterService.getRequestBizContent(requestDto);
			_log.info("请求{}，业务参数：{}", "confirmVerifyCode", reqBizContent);
			resBizContent = payService.gateVerifyCode(request, requestDto, reqBizContent, responseDto);
			_log.info("{}业务处理结果：{}", "confirmVerifyCode", resBizContent);
			/*应答参数签名加密*/
			String respData = baseInterService.getResponseBizContent(null, resBizContent);
			responseDto.setData(respData);
		}catch(Exception e){
			_log.error("订单查询服务发生异常",e);
			responseDto.setRsp_code(SystemConstant.RSPCODE_ERROR);
			responseDto.setRsp_msg(SystemConstant.RSPCODE_ERROR_MSG);
		}finally{
			baseInterService.saveLog(requestDto, responseDto, reqBizContent, resBizContent, "payGateRequest");
		}
		_log.info("应答{}，报文：{}", "confirmVerifyCode", responseDto.toString());
		return responseDto;
	}
	
	/**
	 * 接口: 外接支付控制层
	 * @param request 请求对象
	 * @param requestDto  请求实体
	 * @return
	 */
	@RequestMapping(value = "/queryGateResult.do", method = RequestMethod.POST)
	public @ResponseBody ResponseDTO queryGateResult(HttpServletRequest request, RequestDTO requestDto){				
		ResponseDTO responseDto = new ResponseDTO();	
		Map<String, String> reqBizContent = null, resBizContent = null;
		try{
			_log.info("请求{}，报文：{}", "queryGateResult", requestDto.toString());
			/*提取业务请求参数*/
			reqBizContent = baseInterService.getRequestBizContent(requestDto);
			_log.info("请求{}，业务参数：{}", "queryGateResult", reqBizContent);
			resBizContent = payService.queryGateService(request, requestDto, reqBizContent, responseDto);
			_log.info("{}业务处理结果：{}", "queryGateResult", resBizContent);
			/*应答参数签名加密*/
			String respData = baseInterService.getResponseBizContent(null, resBizContent);
			responseDto.setData(respData);
		}catch(Exception e){
			_log.error("订单查询服务发生异常",e);
			responseDto.setRsp_code(SystemConstant.RSPCODE_ERROR);
			responseDto.setRsp_msg(SystemConstant.RSPCODE_ERROR_MSG);
		}finally{
			baseInterService.saveLog(requestDto, responseDto, reqBizContent, resBizContent, "payGateRequest");
		}
		_log.info("应答{}，报文：{}", "queryGateResult", responseDto.toString());
		return responseDto;
	}

}
